//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System;
using System.Reflection;
using System.ServiceModel;
using System.ComponentModel;
using System.Drawing.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Validation;
using EntLibContrib.Common.Configuration.Design;
using EntLibContrib.Common.Configuration.Design.Editors;
using EntLibContrib.Query.Properties;

namespace EntLibContrib.Query.Configuration.Design
{
	/// <summary>
	/// Represents a query using a Wcf Service.
	/// </summary>
	public sealed class WcfServiceQueryNode : ServiceQueryNode
	{
		#region Fields
		private string endpoint;
		#endregion

		#region Properties
		/// <summary>
		/// Gets or sets the end point.
		/// </summary>
		/// <value>The end point for this node.</value>
		[Required]
		[QueryCategory("CategoryProvider")]
		[QueryDescription("WcfServiceQueryEndpointDescription")]
		public string Endpoint
		{
			get { return endpoint; }
			set
			{
				if (value != null)
					endpoint = value;
			}
		}

		/// <summary>
		/// Gets or sets the name of the service.
		/// </summary>
		/// <value>The service name for this node.</value>
		[Required]
		[Editor(typeof(MethodSelectorEditor), typeof(UITypeEditor))]
		[BaseType(typeof(ICommunicationObject), TypeSelectorIncludes.None)]
		[MethodFilter(BindingFlags.DeclaredOnly|BindingFlags.Instance|BindingFlags.Public)]
		public override TypeMethodName TypeMethodName
		{
			get { return base.TypeMethodName; }
			set { base.TypeMethodName = value; }
		}
		/// <summary>
		/// Gets a <see cref="WcfServiceQueryData"/> configuration object using the node data.
		/// </summary>
		/// <value>
		/// A <see cref="WcfServiceQueryData"/> configuration object using the node data.
		/// </value>
		[ReadOnly(true)]
		public override QueryDataBase QueryData
		{
			get { return new WcfServiceQueryData(Name, CustomCommandName, ParameterSetName, Address, TypeMethodName.TypeName, TypeMethodName.MethodName, Endpoint); }
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initialize a new instance of the <see cref="WcfServiceQueryNode"/> class.
		/// </summary>
		public WcfServiceQueryNode ()
			: this(new WcfServiceQueryData(InternalResources.DefaultWcfServiceQueryNodeName, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty))
		{
		}

		/// <summary>
		/// Initialize a new instance of the<see cref="WcfServiceQueryNode"/> class with a <see cref="WcfServiceQueryData"/> configuration object.
		/// </summary>
		/// <param name="wcfServiceQueryData">The wcf service query data.</param>
		public WcfServiceQueryNode (WcfServiceQueryData wcfServiceQueryData)
		{
			if (wcfServiceQueryData == null)
				throw new ArgumentNullException("wcfServiceQueryData");

			Rename(wcfServiceQueryData.Name);
			base.CustomCommandName = wcfServiceQueryData.CustomCommandName;
			base.ParameterSetName = wcfServiceQueryData.ParameterSetName;
			this.Address = wcfServiceQueryData.Address;
			base.TypeMethodName = new TypeMethodName(wcfServiceQueryData.Service, wcfServiceQueryData.Method);
			this.endpoint = wcfServiceQueryData.Endpoint;
		}
		#endregion
	}
}